package org.myh.excel;


import org.myh.excel.concurrent.AbstractDivisibleExcelBuffer;
import org.myh.excel.pojo.BuildDataSource;
import org.myh.excel.support.CRECallBackDataWithDBConnect;

/**
 * ClassName: ConcurrentExcelBufferBuilder
 * Description:
 * date: 2024/3/26 9:44
 *
 * @author Yu Han
 * @since JDK 1.8
 */
public class ConcurrentExcelBufferBuilder {

    /**
     * 线程池线程数
     */
    private int threadSize = 20;
    /**
     * 类型默认不不返回connect 使用者自己采用mybatis或其他方式处理
     */
    private int type = CRECallBackDataWithDBConnect.MYBATIS_TYPE;
    /**
     * 构建JDBC数据资源
     */
    private BuildDataSource source;
    /**
     * 打开前置处理
     */
    private boolean openPreHandle = false;
    /**
     * 打开后置处理
     */
    private boolean openPostHandle = false;

    /**
     * excelBuffer对象 默认可不传（便于spring声明bean），但执行器期间需设置
     */
    private AbstractDivisibleExcelBuffer excelBuffer;

    private ConcurrentExcelBufferBuilder() {
    }


    public static ConcurrentExcelBufferBuilder instance() {
        return BuilderHolder.HOLDER.instance;
    }

    public ConcurrentExcelBufferBuilder useThreadSize(int threadSize) {
        this.threadSize = threadSize;
        return this;
    }

    public ConcurrentExcelBufferBuilder excelProcessor(AbstractDivisibleExcelBuffer excelBuffer) {
        this.excelBuffer = excelBuffer;
        return this;
    }

    public ConcurrentExcelBufferBuilder addDataSource(BuildDataSource dataSource) {
        this.source = dataSource;
        return this;
    }

    public ConcurrentExcelBufferBuilder openPreHandle(boolean openPreHandle) {
        this.openPreHandle = openPreHandle;
        return this;
    }

    public ConcurrentExcelBufferBuilder openPostHandle(boolean openPostHandle) {
        this.openPostHandle = openPostHandle;
        return this;
    }

    /**
     * 枚举类型
     *
     * @param type
     * @return
     */
    public ConcurrentExcelBufferBuilder useType(int type) {
        this.type = type;
        return this;
    }

    public CRECallBackDataWithDBConnect build() {
        CRECallBackDataWithDBConnect cre = null;
        if (this.threadSize <= 0) {
            this.threadSize = 20;
        }
        if (type == CRECallBackDataWithDBConnect.JDBC_TYPE) {
            cre = new CRECallBackDataWithDBConnect(this.threadSize, this.type, this.source.getDriver(),
                    this.source.getProperties(),this.openPreHandle,this.openPostHandle);
        }

        if (type == CRECallBackDataWithDBConnect.MYBATIS_TYPE) {
            cre = new CRECallBackDataWithDBConnect(this.threadSize, this.type,this.openPreHandle,this.openPostHandle);
        }

        if (cre == null) {
            throw new RuntimeException("创建CRECallBackDataWithDBConnect失败");
        }

        if (this.excelBuffer != null) {
            cre.setDivisibleExcelBuffer(this.excelBuffer);
        }

        return cre;
    }

    public static void main(String[] args) {
        ConcurrentExcelBufferBuilder bufferBuilder = new ConcurrentExcelBufferBuilder();
    }

    /**
     * 返回单例
     */
    private enum BuilderHolder {
        /**
         * 持有者单例
         */
        HOLDER;
        private ConcurrentExcelBufferBuilder instance;

        BuilderHolder() {
            instance = new ConcurrentExcelBufferBuilder();
        }
    }


}
